package com.stock.demo.function.mine.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.stock.demo.R;
import com.stock.demo.framework.ui.view.StockBaseDialog;
import com.stock.demo.util.SettingUtils;

/**
 * Created by XuZhaoyu on 2017/4/14.
 */

public class SpeedChooseDialog extends StockBaseDialog {

    private View mView;

    private FrameLayout mFlFirst;
    private ImageView mIvFirst;

    private FrameLayout mFlSecond;
    private ImageView mIvSecond;

    private FrameLayout mFlThird;
    private ImageView mIvThird;

    private FrameLayout mFlFourth;
    private ImageView mIvFourth;

    private FrameLayout mFlFifth;
    private ImageView mIvFifth;

    public SpeedChooseDialog(Context context) {
        super(context);
    }

    @Override
    public void initWindow(WindowManager windowManager, Window window) {
        window.setGravity(Gravity.CENTER);
        int width = windowManager.getDefaultDisplay().getWidth();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int) (width * 0.6);
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
    }

    @Override
    public boolean setDialogCancelable() {
        return true;
    }

    @Override
    public View setDialogView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_speed_choose_dialog, null);

        mFlFirst = (FrameLayout) mView.findViewById(R.id.fl_five_item_first);
        mIvFirst = (ImageView) mView.findViewById(R.id.iv_five_item_first);

        mFlSecond = (FrameLayout) mView.findViewById(R.id.fl_five_item_second);
        mIvSecond = (ImageView) mView.findViewById(R.id.iv_five_item_second);

        mFlThird = (FrameLayout) mView.findViewById(R.id.fl_five_item_third);
        mIvThird = (ImageView) mView.findViewById(R.id.iv_five_item_third);

        mFlFourth = (FrameLayout) mView.findViewById(R.id.fl_five_item_fourth);
        mIvFourth = (ImageView) mView.findViewById(R.id.iv_five_item_fourth);

        mFlFifth = (FrameLayout) mView.findViewById(R.id.fl_five_item_fifth);
        mIvFifth = (ImageView) mView.findViewById(R.id.iv_five_item_fifth);

        initView();

        return mView;
    }

    private void initView() {
        changeSelection(SettingUtils.getRefreshSpeed());
        mFlFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.setRefreshSpeed(SettingUtils.SPEED_VERY_FAST);
                dismiss();
            }
        });

        mFlSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.setRefreshSpeed(SettingUtils.SPEED_FAST);
                dismiss();
            }
        });

        mFlThird.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.setRefreshSpeed(SettingUtils.SPEED_MEDIUM);
                dismiss();
            }
        });

        mFlFourth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.setRefreshSpeed(SettingUtils.SPEED_SLOW);
                dismiss();
            }
        });

        mFlFifth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.setRefreshSpeed(SettingUtils.SPEED_VERY_SLOW);
                dismiss();
            }
        });
    }

    private void changeSelection(int speed) {
        mIvFirst.setVisibility(View.INVISIBLE);
        mIvSecond.setVisibility(View.INVISIBLE);
        mIvThird.setVisibility(View.INVISIBLE);
        mIvFourth.setVisibility(View.INVISIBLE);
        mIvFifth.setVisibility(View.INVISIBLE);
        switch (speed) {
            case SettingUtils.SPEED_VERY_FAST:
                mIvFirst.setVisibility(View.VISIBLE);
                break;
            case SettingUtils.SPEED_FAST:
                mIvSecond.setVisibility(View.VISIBLE);
                break;
            case SettingUtils.SPEED_MEDIUM:
                mIvThird.setVisibility(View.VISIBLE);
                break;
            case SettingUtils.SPEED_SLOW:
                mIvFourth.setVisibility(View.VISIBLE);
                break;
            case SettingUtils.SPEED_VERY_SLOW:
                mIvFifth.setVisibility(View.VISIBLE);
                break;
        }
    }

}
